package com.punai.monthanalysis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.monthanalysis.bo.ProYdcbfxZyfbgcdbfxbBo;
import com.punai.monthanalysis.domain.ProYdcbfxZyfbgcdbfxb;
import com.punai.monthanalysis.mapper.ProYdcbfxZyfbgcdbfxbMapper;
import com.punai.monthanalysis.service.IProYdcbfxZyfbgcdbfxbService;
import com.punai.monthanalysis.sqliteDao.CostStructureCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 专业分包工程对比分析Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxZyfbgcdbfxbServiceImpl extends ServiceImpl<ProYdcbfxZyfbgcdbfxbMapper,ProYdcbfxZyfbgcdbfxb> implements IProYdcbfxZyfbgcdbfxbService
{
    @Autowired
    private ProYdcbfxZyfbgcdbfxbMapper proYdcbfxZyfbgcdbfxbMapper;
    @Autowired
    private CostStructureCategoryRepository costStructureCategoryRepository;

    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {
        ListValueMap<String,YdCbfeeiteminfoStandard> listValueMap = new ListValueMap<>();
        list.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        categorysList.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        List<YdCbfeeiteminfoStandard> roots = listValueMap.get("0");
        List<ProYdcbfxZyfbgcdbfxbBo> result = new ArrayList<>();

        if (CollUtil.isNotEmpty(roots)){
            for (YdCbfeeiteminfoStandard root : roots) {
                ProYdcbfxZyfbgcdbfxbBo convert = convert(root);
                result.add(convert);
                setChildren(convert,listValueMap);
            }
        }

        return result;
    }

    private void setChildren(ProYdcbfxZyfbgcdbfxbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap) {

        String id = parent.getId();
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxZyfbgcdbfxbBo convert = convert(child);
                parent.getChildren().add(convert);
                setChildren(convert,listValueMap);
            }
        }
    }

    private ProYdcbfxZyfbgcdbfxbBo convert(YdCbfeeiteminfoStandard in){
        ProYdcbfxZyfbgcdbfxbBo out = new ProYdcbfxZyfbgcdbfxbBo();
        out.setId(in.getOid()+"");
        out.setProId(in.getProId());
        out.setQc(in.getYdDate());//期次Id
        out.setItemName(in.getName());
        out.setUnit(in.getUnit());

        out.setYsQuantity(in.getMonthAmount());
        out.setYsAvgPrice(in.getMonthPrice());
        out.setYsPrice(in.getMonthCzTotal());

        out.setSjQuantity(in.getActualCostAmout());
        out.setSjAvgPrice(in.getActualCostNoTaxPrice());
        out.setSjPrice(in.getActualCostNoTaxTotal());

        BigDecimal ysAmount = in.getMonthAmount();
        BigDecimal ysTotal = in.getMonthCzTotal();
        BigDecimal jcQuantity = in.getDifferenceOfCzsjAmount();
        BigDecimal quantityJcl = NumberUtil.divideNew(jcQuantity, 2, ysAmount);
        BigDecimal price = in.getKlDifferenceOfCzsjTotal();
        BigDecimal priceJcl = NumberUtil.divideNew(price, 2, ysTotal);
        out.setJcQuantity(jcQuantity);
        out.setQuantityJcl(quantityJcl);
        out.setPrice(price);
        out.setPriceJcl(priceJcl);
        return out;
    }
}
